package com.eppear.jpfm.framework.context;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.ApplicationContext;

import com.eppear.jpfm.framework.application.ApplicationUtils;
import com.eppear.jpfm.framework.common.ConfigConstants;
import com.eppear.jpfm.framework.constants.Constants;
import com.eppear.jpfm.framework.model.TWxAccount;




/**
 * @ClassName Context
 * @Description 系统上下文方法类,用于存储用应程序级的运行时对象
 * @author JianFeng Zhang(张建锋)
 * @date 2016-12-12 下午10:49:19
 *
 */
public class Context {

	/**
	 * 系统缓存
	 */
	private static Map cache = null;

	
	/**
	 * @Title getCacheDate
	 * @Description 根据Key获取缓存的数据对象
	 * @param key 关键字
	 * @return Object
	 */
	public static Object getCacheDate(Object key){
		return cache.get(key);
	}
	/**
	 * @Title getDicCacheData
	 * @Description 根据KEY 获取字典缓存的值
	 * @param key
	 * @return
	 */
	public static Object getDicCacheData(Object key){
		Map dicCache = getDicCache();
		if(dicCache == null){
			return null;
		}
		return dicCache.get(key);
	}
	
	/**
	 * @Title getDicCache
	 * @Description 获取字典
	 * @return
	 */
	public static Map getDicCache(){
		Map dicCache = (Map)cache.get(ConfigConstants.CACHE_DIC_KEY);
		return dicCache;
	}
	/**
	 * @Title getParamCache
	 * @Description 获取参数缓存
	 * @return Map
	 */
	public static Map getParamCache(){
		Map paramCache = (Map)cache.get(ConfigConstants.CACHE_SYS_PARAM_KEY);
		return paramCache;
	}
	/**
	 * @Title getParamCacheData
	 * @Description 根据KEY 获取参数缓存的值
	 * @param key
	 * @return
	 */
	public static Object getParamCacheData(Object key){
		Map paramCache = getParamCache();
		return paramCache.get(key);
	}
	
	public static ApplicationContext getApplicationContext(){
		return ApplicationUtils.getApplicationContext();
	}
	/**
	 * @Title getBean
	 * @Description 获取指定名字的bean
	 * @param name
	 * @return
	 */
	public Object getBean(String name){
		return ApplicationUtils.getBean(name);
	}

	public static Map getCach() {
		return cache;
	}
	
	public synchronized static void  setCacheData(Object key, Object value){
		cache.put(key, value);
	}

	/**
	 *  删除指定元素
	 * @param key
	 */
	public synchronized static void  removeCacheData(Object key){
		cache.remove(key);
	}
	
	public synchronized static void setCache(Map cache) {
		Context.cache = cache;
	}

	/**
	 *   获取缓存中所有的微信账号信息
	 * @author JianFeng Zhang(张建锋) 
	 * @data   2017-04-16 下午9:04:07
	 * @return
	 */
	public static Map<Integer,TWxAccount> getWxAccs(){
		@SuppressWarnings("unchecked")
		Map<Integer,TWxAccount> wxAccs =  (Map<Integer,TWxAccount>)Context.getCacheDate(Constants.WX_ACCOUNT_LISTMAP);
		return wxAccs;
	}

	/**
	 *   根据ID,从缓存中获取具体微信账号实体
	 * @author JianFeng Zhang(张建锋) 
	 * @data   2017-04-16 下午9:10:05
	 * @param id
	 * @return
	 */
	public static  TWxAccount getWxAcc(Integer id){
		TWxAccount wxAcc = null;
		Map<Integer,TWxAccount> wxAccs = getWxAccs();
		if ( wxAccs != null){
			wxAcc = wxAccs.get(id);
		}
		return wxAcc;
	}
	
	/**
	 *   获取缓存中所有的微信账号信息
	 * @author JianFeng Zhang(张建锋) 
	 * @data   2017-04-16 下午9:04:07
	 * @return
	 */
	public synchronized static void setWxAccs(Map<Integer,TWxAccount> wxAccs){
		setCacheData(Constants.WX_ACCOUNT_LISTMAP,wxAccs);
	}
	
	/**
	 *   设置或更一个账户信息
	 * @author JianFeng Zhang(张建锋) 
	 * @data   2017-04-16 下午9:34:21
	 * @param wxAcc
	 */
	public synchronized static void setWxAcc(TWxAccount wxAcc){
		Integer id = wxAcc.getId();
		Map<Integer,TWxAccount> wxAccs = getWxAccs();
		if (wxAccs != null)
			wxAccs.put(id, wxAcc);
		else{
			wxAccs = new HashMap<Integer,TWxAccount>();
			wxAccs.put(id, wxAcc);			
		}
		setWxAccs(wxAccs);
	}
	
	/**
	 *    移除缓存中指定ID的账户信息
	 * @author JianFeng Zhang(张建锋) 
	 * @data   2017-04-16 下午9:35:53
	 * @param id
	 */
	public synchronized static void removeWxAcc(Integer id){
		Map<Integer,TWxAccount> wxAccs = getWxAccs();
		wxAccs.remove(id);
	}
}
